package com.xjscrm.console.config.datasource;

import com.xjscrm.common.entity.SysTenant;
import com.xjscrm.console.biz.SysTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

/**
 * 初始化动态数据源
 */
@Slf4j
@Configuration
public class DynamicDataSourceInit {

    @Resource
    private SysTenantService sysTenantService;

    @Resource
    private DynamicDataSource dynamicDataSource;


    @PostConstruct
    public void initDataSource() throws SQLException {
        log.info("tenant datasource init..");
        // 初始化租户数据源
        List<SysTenant> tenantList = sysTenantService.getAll();
        for (SysTenant tenant : tenantList) {
            dynamicDataSource.setDataSources(tenant);
        }
    }
}
